<!-- BookSVT/BookProject/src/components/NewBook.vue -->
<template>
  <div class="container">
    <h3>新建书籍</h3>
    <el-form :model="book" label-width="120px">
      <el-form-item label="书名">
        <el-input v-model="book.name"></el-input>
      </el-form-item>
      <el-form-item label="类型">
        <el-input v-model="book.type"></el-input>
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="book.information"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { ElMessage } from 'element-plus';

const router = useRouter()
const book = ref({ name: '', type: '' ,information: ''})

// 提交表单
const submitForm = async () => {
  try {
    await axios.post('http://localhost:8080/book', book.value)
    ElMessage.success('新增成功')
    router.push({ name: 'books' })
  } catch (error) {
    console.error('新增失败:', error)
    ElMessage.error('新增失败')
  }
}

// 取消新建
const cancel = () => {
  router.go(-1)
}
</script>